package com.twosnail.frame.controller;

import com.jfinal.core.Controller;
import com.jfinal.ext.render.excel.PoiRender;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.Render;
import com.twosnail.frame.commin.util.result.ResultObj;
import com.twosnail.frame.model.CouponUseLog;
import com.twosnail.frame.model.Gym;
import com.twosnail.frame.model.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import java.util.List;

/**
 * Created by yangcs on 2017/7/21.
 */
public class GymController extends Controller {
    private Logger logger = Logger.getLogger(GymController.class) ;

    @RequiresPermissions("GymController")
    public void index(){
        try {
            setAttr( "list", Gym.me.getAllGyms());
            setAttr( "gyms" , Gym.me.getGyms() );
        } catch (Exception e) {
            this.logger.warn( "门店列表信息初始化失败！" , e );
        }
        render( "gym_list.html" );
    }

    @RequiresPermissions("GymController")
    public void list(){
        try {
            long gymId = getParaToLong("gymId") ;
            setAttr("gym", Gym.me.getGymById(gymId));
            setAttr("list",Gym.me.getAllMd(gymId));
        } catch (Exception e) {
            this.logger.warn( "门店列表信息初始化失败！" , e );
        }
        render( "md_list.html" );
    }

    @RequiresPermissions("GymController")
    public void useList(){
        try {
            long userId = getParaToLong("userId") ;
            Integer pageNum = getParaToInt( "pageNum" ) ;
            Integer numPerPage = getParaToInt( "numPerPage" ) ;
            pageNum = pageNum == null ? 1 : pageNum;
            numPerPage = ( numPerPage == null || numPerPage == 0 ) ? 5 : numPerPage;
            Page<Record> list = CouponUseLog.me.getUseLogById( userId , pageNum, numPerPage );
            setAttr( "list", list );
            setAttr( "userId", userId );
        } catch (Exception e) {
            this.logger.warn( "使用列表信息初始化失败！" , e );
        }
        render( "use_list.html" );
    }

    /**
     * 新增团购
     */
    @RequiresPermissions("GymController.addCount")
    public void addCount(){
        long gymId = getParaToLong("gymId") ;
        int count = getParaToInt("count");
        try {
            Gym.me.updateCount(gymId,count);
            renderJson( new ResultObj( ResultObj.SUCCESS , "添加成功" , null ) );
        } catch ( Exception e ) {
            this.logger.info( "系统异常，添加团购次数失败！", e );
            renderJson( new ResultObj( ResultObj.FAIL , e.getMessage() , null ) );
        }
    }

    /**
     * 新增门店
     */
    @RequiresPermissions("GymController.addMd")
    public void addMd(){
        long gymId = getParaToLong("gymId") ;
        String userName = getPara("userName");
        String neckName = getPara("neckName");
        String passWord = getPara("passWord");
        String mobile = getPara("mobile");
        String addr = getPara("addr");
        try {
            SysUser.me.addMd(gymId,userName,neckName,passWord,addr,mobile,getSession(),getRequest());
            renderJson( new ResultObj( ResultObj.SUCCESS , "添加成功" , null ) );
        } catch ( Exception e ) {
            this.logger.info( "系统异常，添加门店失败！", e );
            renderJson( new ResultObj( ResultObj.FAIL , e.getMessage() , null ) );
        }
    }

    /**
     * 重置密码
     */
    public void updatePs(){
        long userId = getParaToLong("userId") ;
        String passWord = getPara("passWord");
        try {
            SysUser.me.updatePs(userId,passWord);
            renderJson( new ResultObj( ResultObj.SUCCESS , "重置成功" , null ) );
        } catch ( Exception e ) {
            this.logger.info( "系统异常，重置密码失败！", e );
            renderJson( new ResultObj( ResultObj.FAIL , e.getMessage() , null ) );
        }
    }

    /**
     * 修改信息
     */
    public void updateInfo(){
        long userId = getParaToLong("userId") ;
        String neckName = getPara("neckName");
        String addr = getPara("addr");
        String mobile = getPara("mobile");
        try {
            SysUser.me.updateInfo(userId,neckName,addr,mobile);
            renderJson( new ResultObj( ResultObj.SUCCESS , "修改成功" , null ) );
        } catch ( Exception e ) {
            this.logger.info( "系统异常，修改信息失败！", e );
            renderJson( new ResultObj( ResultObj.FAIL , e.getMessage() , null ) );
        }
    }

    /**
     * 导出所有
     */
    @RequiresPermissions("GymController.exportAll")
    public void exportAll(){
        long gymId = getParaToLong("gymId") ;
        List<Record> list = CouponUseLog.me.getListByGymId(gymId);
        String[] header={"流水号","验证时间","验证码","门店名"};
        String[] columns={"orderCode","useTime","checkCode","neckName"};
        //其实关键代码就这一句话fileName、sheetName、columns、headers为必须
        Render poirender = PoiRender.me(list).fileName(System.currentTimeMillis()+".xls").headers(header).sheetName("健身劵使用记录").columns(columns);
        render(poirender);
    }

    /**
     * 导出
     */
    @RequiresPermissions("GymController.export")
    public void export(){
        long userId = getParaToLong("userId") ;
        List<Record> list = CouponUseLog.me.getListById(userId);
        String[] header={"流水号","验证时间","验证码","门店名"};
        String[] columns={"orderCode","useTime","checkCode","neckName"};
        //其实关键代码就这一句话fileName、sheetName、columns、headers为必须
        Render poirender = PoiRender.me(list).fileName(System.currentTimeMillis()+".xls").headers(header).sheetName("健身劵使用记录").columns(columns);
        render(poirender);
    }
}
